package com.baidu.infinity.ui.fragment.home.shape.shapes

import android.graphics.Canvas
import android.graphics.Paint
import android.os.Build
import androidx.annotation.RequiresApi
import com.baidu.infinity.ui.fragment.home.shape.BaseShape
import com.baidu.infinity.ui.util.state.ShapeState

class CasualShape: BaseShape() {
    override fun setStartPoint(startX: Float, startY: Float) {
        super.setStartPoint(startX, startY)
        mPath.moveTo(startX,startY)
        rectF.left = startX
        rectF.top = startY
        rectF.right = startX
        rectF.bottom = startY
    }

    override fun setEndPoint(endX: Float, endY: Float) {
        this.endX = endX
        this.endY = endY
        this.centerX = (startX + endX) / 2
        this.centerY = (startY + endY) / 2
        rectF.left = Math.min(rectF.left,endX)
        rectF.top = Math.min(rectF.top,endY)
        rectF.right = Math.max(rectF.right,endX)
        rectF.bottom = Math.max(rectF.bottom,endY)
        if (mState == ShapeState.DRAWING){
            mPath.lineTo(endX,endY)
        }
    }
    @RequiresApi(Build.VERSION_CODES.VANILLA_ICE_CREAM)
    override fun draw(canvas: Canvas) {
        super.draw(canvas)

        mPaint.style = Paint.Style.STROKE
        canvas.drawPath(mPath,mPaint)
    }

    override fun calculateLotation(x: Float, y: Float) {
        mMoveDX = x - mStartMoveX
        mMoveDY = y - mStartMoveY
        if(mSelectedState == SelectState.CENTER){
            mPath.offset(mMoveDX,mMoveDY)
            rectF.offset(mMoveDX,mMoveDY)
        }
        mStartMoveX = x
        mStartMoveY = y
    }

    override fun updateSelectState(x: Float, y: Float) {
        mStartMoveX = x
        mStartMoveY = y
        mSelectedState = SelectState.CENTER
    }

    override fun containsPoint(x: Float, y: Float): Boolean {
        return super.containsPoint(x, y)
    }
}